//
//  UIView+ZFYType.m
//  RunTime
//
//  Created by 张飞跃 on 16/5/27.
//  Copyright © 2016年 冰泪. All rights reserved.
//

#import "UIView+ZFYType.h"
#import <objc/runtime.h>
#undef   KEY_TAGSTRING
#define KEY_TAGSTRING     "tagString"

@implementation UIView (ZFYType)
@dynamic model;
@dynamic tagString;

- (ViewTypeString *)model {
    NSObject *obj = objc_getAssociatedObject(self, KEY_TAGSTRING);
    
    if (obj && [obj isKindOfClass:[ViewTypeString class]]) {
        
        return (ViewTypeString *)obj;
        
    }
    return nil;
}
-(void)setModel:(ViewTypeString *)model
{
    objc_setAssociatedObject(self, KEY_TAGSTRING, model, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

- (NSString *)tagString {
    NSObject *obj = objc_getAssociatedObject(self, KEY_TAGSTRING);
    
    if (obj && [obj isKindOfClass:[NSString class]]) {
        
        return (NSString *)obj;
    }
    return nil;
}
- (void)setTagString:(NSString *)value {
    
    objc_setAssociatedObject(self, KEY_TAGSTRING, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)viewWithTagString:(NSString *)value {
    if (nil == value) {
        return nil;
    }
    for (UIView *subview in self.subviews) {
        NSString *tag = subview.tagString;
        if ([tag isEqualToString:value])
        {
            return subview;
        }
    }
    return nil;
}

-(void)removeAssociatedObjects
{
    objc_setAssociatedObject(self, KEY_TAGSTRING, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);//移除当前对象
   // 使用函数objc_removeAssociatedObjects可以断开所有associative。通常情况下不建议这么做，因为他会断开所有关联。
}

@end
